package com.chen.gulimall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.chen.gulimall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    // 应用ID,您的APPID，收款账号既是您的APPID对应支付宝账号
    public String app_id = "2021000121662271";

    // 商户私钥，您的PKCS8格式RSA2私钥
    public String merchant_private_key = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCK5QzjRYDsBbpT7/ajqrnk7+Rbtcup65TflZ7xheVtDMpskT8cGZ28BS0LDPrOmyIyaDKK1hmgS31bi66rgGhDxEDlttgTFpqqJTCatJdQIGNZLBEqL43WOcKDZPUTRZYm+yYw5reU9qPcjvzNBhzzvMRhPNzTQCTwQzgJcA6pooogL1y6n3e/0pS/Ox2JOpGAHrlAuC/WZ/kjfl49caL9bnuz8kqfFECl4c1vZdhao6IXZJNnJDIdnkZDJuLdhGcs4waISPgX7sw/V0lMW4Vn8UtdexrJnRNqBE2Fb2Ax6ktYRjGT4tzmj185UZho6gd47DCrmRxfJL1n7d0YJDlBAgMBAAECggEAUWBDfcCR+Tad5WG5UkjLoys90tGfYo2o6pohxoXZVlvhGVy76f3EIY2yxz+yjmvrzDaqLhekafrwljsvekHAVLvd/fhzfjOHKFqjsHBQGtzu+CbOY7X61uF+ajczMzhY8bvBs3Lpajha5vVLfd+wBMm7EPF0u7xgJ/hqE3V40b+RKYtJkZP7A3CqZvJTek+ZOsK8nBnCUaqPVk/KnQ85zH4dQUSz24/92prJTdtWfcdJW38418blGIPDptFL+Ctg9m0z094EomkoGdK3Q3R+6SM8q8ZNklpx869VW9Ap564kmMPb4iTQdpcnOLa8CsoURSnyLXESXi2EL+BTpxW4cQKBgQDD3da1n/jGj2FiXCYY2bS2y+hJamilNwkVd2Ye59s1XkdAulhRZmk9wwBwnTSBprmFhFBp/6O9F/91q4E1Okv92rcyikaE4SHwNeBE2z4JNni5JmWwd7LLfDa3a9pZQ1fUVnQ05xzmFC38YpjEurarq6HGAt1Bq3KHr038Uj80JQKBgQC1iYCP0QdE4ncC/GVZMdUhVyVF5tDy5P81YSXjkydLNtbJKCpWWMxWeAOt4F6bR9xiCpj5/L3qhZCLwaabPxH5ZL6w8vMh1pL4udwjVG2JHeAIqhKjszj/mIIBvZ4+NerZlQPwnt/NV6/M1afQV+gJ+wayG9kJsMvzDak8N6k37QKBgEdAj1gZLuYEyNeAUDegExagjQsQE6tunvZZLiQ2Ek+S0XbhkzlheQ2Hv8Q33B8CKOFCZgJ5hfZHIZ6L2oAjift6MkTHeqniQHdjEgfaffHaCAbKyhltIfTWZg1Jxe/NCMls/e802BV259e1gn+msMc4s6wV5Kl8Fjj38/XDccylAoGBAIiw33fq2cFZkGlUKO/vYN744lzu/phWK4BDBSeFjwnIYhQXRUG9HSdiuMp87l+XzW6ZMjTRxSLIrV0+VDgDVoJmBFvyo3XwPrxAHm0PzmZ3c1Cy1tQiSCHU8Ndksrl/GbX5yXyt9DCG/R2MJw7ukg6p0JHd/B+a2wbJW4u3EZjFAoGBAL3nTqGOSYISELbUf0Gmpzu682lnRfg/bjhqh05vKB+FCmeiUrXH+s15YOtYkQ9xyePI3JC6AAX71CL4SBiyyIWCh3uhWEnKHSAilvgBpsFdik/p6ZH5tDBmSjizfZqpY3PaukgC/idtSCDIcOItkDq0h7gtQusboXLCpl2ArNet";

    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    public String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEApn/yOgTKKRJfn76YUpGxP8OK7Li5DKLeqlnEEjzNQbSr7G7iFBhXIT7+Tw4WtgP5JIdvcTXdr8HpzNltLXyYdaE9kXq7ZWqjFf/EMwmkjvGVJugg0crnpd+xVT2tX5TnClWhgDe+iwFz7+WFS44azacRYJGtE4WpoJabphjU5t0uCyXVbNGA+UI8UBxo8dphP861a2i/aBNZH6n3UKY8Uz+VrwHXy25UmqWjJCUsEg2afZNxovlfLKtwcvX2bga0h2vOr25TQHDxqG6msSMFciI1lVplUtrPN/BoHQcLDUzsefA+2NQsQoXLiKUL3QLRxr70MHM+TmVJcLxGH545UQIDAQAB";

    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    public String notify_url = "http://localhost:8080/alipay.trade.page.pay-JAVA-UTF-8/notify_url.jsp";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    public String return_url = "http://member.gulimall.com/memberOrder.html";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    //订单超时时间
    private String timeout = "1m";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    public String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
